import { Injectable } from "@nestjs/common";
import { CharacterAttributeService } from "../services/character-attribute.service";
import { CharacterEquipmentService } from "../services/character-equipment.service";

@Injectable()
export class CharacterAttributeWsHandlers {
  constructor(
    private characterAttributeService: CharacterAttributeService,
    private characterEquipmentService: CharacterEquipmentService
  ) {}

  /**
   * 处理1005指令 - 属性点分配
   * 指令格式: {c: 1005, d: {ap: 力量点数, hp: 体质点数, mp: 智力点数, sp: 敏捷点数}}
   */
  async handleAttributeAllocation(data: any, helpers: any) {
    try {
      const characterId = helpers.selectedCharacterId;
      if (!characterId) {
        return {
          act_id: 1005,
          code: -1,
          msg: "请先选择角色",
        };
      }

      const { ap = 0, hp = 0, mp = 0, sp = 0 } = data;

      // 验证参数
      if (ap < 0 || hp < 0 || mp < 0 || sp < 0) {
        return {
          act_id: 1005,
          code: -1,
          msg: "属性点不能为负数",
        };
      }

      const totalPoints = ap + hp + mp + sp;
      if (totalPoints === 0) {
        // 查询当前属性点信息
        const attributeInfo =
          await this.characterAttributeService.getAttributePoints(characterId);
        return {
          act_id: 1005,
          code: 0,
          data: attributeInfo,
        };
      }

      // 分配属性点
      const result =
        await this.characterAttributeService.allocateAttributePoints(
          characterId,
          ap,
          hp,
          mp,
          sp
        );

      return {
        act_id: 1005,
        code: 0,
        data: {
          // 当前属性值
          ap: result.attributes.ap,
          hp: result.attributes.hp,
          mp: result.attributes.mp,
          sp: result.attributes.sp,
          df: result.attributes.df,
          weight: result.attributes.weight,
          maxHp: result.attributes.maxHp,
          maxMp: result.attributes.maxMp,
          maxWeight: result.attributes.maxWeight,

          // 已分配属性点
          app: result.attributePoints.strength,
          hpp: result.attributePoints.constitution,
          mpp: result.attributePoints.intelligence,
          spp: result.attributePoints.agility,

          // 剩余属性点
          p: result.attributePoints.availablePoints,
        },
      };
    } catch (error: any) {
      console.error("处理属性点分配失败:", error);
      return {
        act_id: 1005,
        code: -1,
        msg: error.message || "属性点分配失败",
      };
    }
  }

  /**
   * 处理1137指令 - 装备穿戴
   * 指令格式: {c: 1137, d: {id: 装备ID}}
   */
  async handleEquipItem(data: any, helpers: any) {
    try {
      const characterId = helpers.selectedCharacterId;
      if (!characterId) {
        return {
          act_id: 1137,
          code: -1,
          msg: "请先选择角色",
        };
      }

      const { id: equipmentId } = data;
      if (!equipmentId) {
        return {
          act_id: 1137,
          code: -1,
          msg: "装备ID不能为空",
        };
      }

      // 检查装备需求
      const requirementCheck =
        await this.characterEquipmentService.checkEquipmentRequirements(
          characterId,
          equipmentId
        );

      if (!requirementCheck.canEquip) {
        return {
          act_id: 1137,
          code: -1,
          msg: "不满足装备需求",
        };
      }

      // 穿戴装备
      const result = await this.characterEquipmentService.equipItem(
        characterId,
        equipmentId
      );

      if (result.success) {
        // 重新计算角色属性
        const newAttributes =
          await this.characterAttributeService.calculateCharacterAttributes(
            characterId
          );

        return {
          act_id: 1137,
          code: 0,
          data: newAttributes,
        };
      } else {
        return {
          act_id: 1137,
          code: -1,
          msg: result.message,
        };
      }
    } catch (error: any) {
      console.error("处理装备穿戴失败:", error);
      return {
        act_id: 1137,
        code: -1,
        msg: error.message || "装备穿戴失败",
      };
    }
  }

  /**
   * 处理1136指令 - 卸下装备
   * 指令格式: {c: 1136, d: {type: 装备类型}}
   */
  async handleUnequipItem(data: any, helpers: any) {
    try {
      const characterId = helpers.selectedCharacterId;
      if (!characterId) {
        return {
          act_id: 1136,
          code: -1,
          msg: "请先选择角色",
        };
      }

      const { type } = data;
      if (type === undefined || type === null) {
        return {
          act_id: 1136,
          code: -1,
          msg: "装备类型不能为空",
        };
      }

      // 装备类型映射
      const typeMapping: Record<number, string> = {
        1: "head", // 头盔
        2: "rightHand", // 武器
        3: "body", // 铠甲
        4: "neck", // 项链
        5: "leftHand", // 护腕
        6: "foot", // 战靴
      };

      const equipmentType = typeMapping[type];
      if (!equipmentType) {
        return {
          act_id: 1136,
          code: -1,
          msg: "无效的装备类型",
        };
      }

      // 卸下装备
      const result = await this.characterEquipmentService.unequipItem(
        characterId,
        equipmentType
      );

      if (result.success) {
        // 重新计算角色属性
        const newAttributes =
          await this.characterAttributeService.calculateCharacterAttributes(
            characterId
          );

        return {
          act_id: 1136,
          code: 0,
          data: newAttributes,
        };
      } else {
        return {
          act_id: 1136,
          code: -1,
          msg: result.message,
        };
      }
    } catch (error: any) {
      console.error("处理装备卸下失败:", error);
      return {
        act_id: 1136,
        code: -1,
        msg: error.message || "装备卸下失败",
      };
    }
  }

  /**
   * 处理1130指令 - 查询角色装备信息
   * 指令格式: {c: 1130, d: {}}
   */
  async handleGetEquipmentInfo(data: any, helpers: any) {
    try {
      const characterId = helpers.selectedCharacterId;
      if (!characterId) {
        return {
          act_id: 1130,
          code: -1,
          msg: "请先选择角色",
        };
      }

      const equipmentInfo =
        await this.characterEquipmentService.getCharacterEquipment(characterId);

      return {
        act_id: 1130,
        code: 0,
        data: equipmentInfo,
      };
    } catch (error: any) {
      console.error("查询装备信息失败:", error);
      return {
        act_id: 1130,
        code: -1,
        msg: error.message || "查询装备信息失败",
      };
    }
  }
}
